﻿using System;
using Jayden.Dll.Core.Persistence;
using Jayden.Dll.Core.Persistence.Attributes;
using Jayden.Dll.Series.Providers;

namespace Jayden.Dll.Series
{
	[Table]
	public class EpisodeInfo : ObjectBase, IComparable<EpisodeInfo>
	{

		private ShowInfo m_ShowInfo;
		private string m_ShowInfoId;
		private int? m_Number;
		private int? m_Season;
		private int? m_Episode;
		private string m_Code;
		private DateTime? m_AirDate;
		private string m_Title;
		private bool m_Special;

		public EpisodeInfo(UnitOfWork uow) : base(uow, 0) { }
		public EpisodeInfo(UnitOfWork  uow, long id) : base(uow, id) { }

		[Field(PrimaryKey = true, AutoIncrement= true)]
		public long PkId { get { return (long)ObjectId[0]; } set { ObjectId[0] = value; } }
		[Field]
		public string ShowInfoId { get { return m_ShowInfoId; } set { m_ShowInfoId = value; } }
		[Field]
		public int? Number { get { return m_Number; } set { m_Number = value; } }
		[Field]
		public int? Season { get { return m_Season; } set { m_Season = value; } }
		[Field]
		public int? Episode { get { return m_Episode; } set { m_Episode = value; } }
		[Field]
		public string Code { get { return m_Code; } set { m_Code = value; } }
		[Field]
		public DateTime? AirDate { get { return m_AirDate; } set { m_AirDate = value; } }
		[Field]
		public string Title { get { return m_Title; } set { m_Title = value; } }
		[Field]
		public bool Special { get { return m_Special; } set { m_Special = value; } }

		public ShowInfo ShowInfo
		{
			get
			{
				if (m_ShowInfo == null && ShowInfoId != null)
				{
					lock (this)
					{
						SQLiteShowInfoDataAccess showProvider = UnitOfWork.DataAccessManager.GetDataAccess<ShowInfo>() as SQLiteShowInfoDataAccess;
						m_ShowInfo = showProvider.FindById(ShowInfoId);
					}
				}
				return m_ShowInfo;
			}
		}

		public int CompareTo(EpisodeInfo other)
		{
			int compare = 0;
			if (AirDate.HasValue && other.AirDate.HasValue)
				compare = AirDate.Value.CompareTo(other.AirDate.Value);
			if (compare == 0 && ShowInfo != null && other.ShowInfo != null)
				compare = ShowInfo.Title.CompareTo(other.ShowInfo.Title);
			if (compare == 0 && Number.HasValue && other.Number.HasValue)
				compare = Number.Value.CompareTo(other.Number.Value);
			if (compare == 0 && Season.HasValue && other.Season.HasValue)
				compare = Season.Value.CompareTo(other.Season.Value);
			if (compare == 0 && Episode.HasValue && other.Episode.HasValue)
				compare = Episode.Value.CompareTo(other.Episode.Value);
			return compare;
		}
	}
}
